package hu.codeguide.xml;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * It represents an item of the RSS channel.
 * 
 * @author David Csejtei, CodeGuide Team, http://codeguide.hu
 * @version 1.0
 */

// TODO: Write a custom implementation of comparator interface. 

public final class RssItem implements Comparable<RssItem> {
        
    /**
     * Required fields of an RSS item.
     */
    private String title;
    private String link;
    private String description;

    /**
     * Optional fields of an RSS item.
     */
    private String pubDate;

    /**
     * A constant to the pubDate format.
     */
    private final SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy.MM.dd.");

    public final String getDate() {
        return pubDate;
    }

    public final void setDate(final Date pubDate) {
        this.pubDate = DATEFORMAT.format(pubDate);
    }

    public final String getDescription() {
        return description;
    }

    public final void setDescription(final String description) {
        this.description = description;
    }

    public final String getTitle() {
            return title;
    }

    public final void setTitle(final String title) {
            this.title = title;
    }

    public final String getLink() {
            return link;
    }

    public final void setLink(final String link) {
            this.link = link;
    }

    @Override
    public String toString() {
        return title+"\n"+pubDate+"\n"+link+"\n"+description+"\n"+hashCode();
        //return title+" | "+link;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        return hash * title.hashCode();
    }

    @Override
    public boolean equals(Object obj) {

        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        final RssItem other = (RssItem) obj;

        if ((this.title == null) ? (other.title != null) : !this.title.equals(other.title)) {
            return false;
        }

        return true;
    }

    /**
     * Comparison by RssItem's title.
     * 
     * Rules:
     * - symmetry
     * - transitivity
     * - consistency with equals
     */
    @Override
    public int compareTo(RssItem another) {
        final int BIGGER = 1;
        final int LESS = -1;
        final int EQUAL = 0;

        if(another.equals(this)) {
            return EQUAL;
        }

        final int result = title.compareTo(another.title);

        if(result != EQUAL) {
            return result;
        }

        return EQUAL;
    }
}
